import axios from "axios";
import { MessageBox, Message } from 'element-ui'
import cookie from 'js-cookie'

const service = axios.create({
  // baseURL: '/api',
  baseURL: 'http://localhost:9001',
  timeout: 20000
})
axios.defaults.timeout = 60000;

service.interceptors.request.use((config) => {
  //函数体
  if (cookie.get("token")) {
    config.headers["Authorization"] = cookie.get("token");
  }
  return config;
},
  (err) => {
    return Promise.reject(err);
  })

service.interceptors.response.use(
  response => {
    //debugger
    if (response.data.code == 28004) {
      console.log("response.data.resultCode是28004")
      // 返回 错误代码-1 清除ticket信息并跳转到登录页面
      //debugger
      window.location.href = "/login"
      return
    } else {
      if (response.data.code !== 20000) {
        //25000：订单支付中，不做任何提示
        if (response.data.code != 25000) {
          Message({
            message: response.data.msg || 'error',
            type: 'error',
            duration: 5 * 1000
          })
        }
      } else {
        return response;
      }
    }
  },
  error => {
    return Promise.reject(error.response)   // 返回接口返回的错误信息
  });

export default service 